package com.jeeplus.mob.common;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SmsSending {
	static final String product = "Dysmsapi";
	static final String domain = "dysmsapi.aliyuncs.com";
	static final String accessKeyId = "LTAI5tGmXWDyzwhEAjt2Mxzt";//替换
	static final String accessKeySecret = "4JlBbJ4mt3N3c6jGJmtfmrzYK3l2sI";//替换

	public static SendSmsResponse sendSms(String phone, String code) throws ClientException {
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");
		//todo 模板需要更换
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
		IAcsClient acsClient = new DefaultAcsClient(profile);
		SendSmsRequest request = new SendSmsRequest();
		request.setPhoneNumbers(phone);
		request.setSignName("新至信车务");//替换
		request.setTemplateCode("SMS_219739848");//替换
		request.setTemplateParam("{code:" + code + "}");
		request.setOutId("");

		SendSmsResponse sendSmsResponse = (SendSmsResponse) acsClient.getAcsResponse(request);
		return sendSmsResponse;
	}

	public static QuerySendDetailsResponse querySendDetails(String bizId, String phone) throws ClientException {
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
		IAcsClient acsClient = new DefaultAcsClient(profile);

		QuerySendDetailsRequest request = new QuerySendDetailsRequest();

		request.setPhoneNumber(phone);

		request.setBizId(bizId);

		SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
		request.setSendDate(ft.format(new Date()));

		request.setPageSize(Long.valueOf(10L));

		request.setCurrentPage(Long.valueOf(1L));

		QuerySendDetailsResponse querySendDetailsResponse = (QuerySendDetailsResponse) acsClient.getAcsResponse(request);

		return querySendDetailsResponse;
	}
}
